/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.antexpress.service;

import com.antexpress.bean.Authentication;
import com.antexpress.bean.Page;
import com.antexpress.common.Response;
import com.antexpress.dao.BillDAO;
import com.antexpress.dao.BillDAOImpl;
import com.antexpress.pojo.Bill;
import com.antexpress.pojo.BillSelector;
import com.antexpress.pojo.CustomerInfo;
import com.antexpress.pojo.Employee;
import com.antexpress.pojo.MapCity;
import com.antexpress.pojo.MapDistrict;
import com.antexpress.pojo.MapProvince;
import com.antexpress.pojo.MapTown;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 * @author Dannis
 */
@Service
public class BillServiceImp implements BillService {

    @Autowired
    private BillDAO billdao;

    @Override
    public Response addNewBill(Bill newbill) {
        boolean flag = billdao.addSendBill(newbill);
        if (flag) {
            return Response.createBySuccessMessage("Create Successful!!");
        } else {
            return Response.createByErrorMessage("Create Failed!!");
        }
    }

    @Override
    public Response getAllBill() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public Response getBillByStatus(int status) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public Response deleteBill(Bill bill) {

        boolean flag = billdao.deleteBill(bill);
        if (flag) {
            return Response.createBySuccessMessage("Delete Success");
        }
        return Response.createByErrorMessage("Delete Failed");
    }

    @Override
    public Response deleteBills(ArrayList<Bill> bills) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public Response updateBillInfo(Bill bill) {
        boolean flag = billdao.updateBill(bill);
        if (flag) {
            return Response.createBySuccessMessage("Update Success");
        }
        return Response.createByErrorMessage("Update Failed");
    }

    @Override
    public Response changeBillStatus(Bill bill, int status) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public Response selectBill(BillSelector select) {
        Response resp = null;
        if (!(select.getStartweight() == 0)) {
            select.setChoose_start_weight(true);
        }
        if (!(select.getEndweight() == 0)) {
            select.setChoose_end_weight(true);
        }
        if (!(select.getStartfreight() == 0)) {
            select.setChoose_start_freight(true);
        }
        if (!(select.getEndfreight() == 0)) {
            select.setChoose_end_freight(true);
        }
        if (!(select.getStatus() == -1)) {
            select.setChoose_status(true);
        }
        if (!(select.getStartcreatetime() == null)) {
            select.setChoose_createtime(true);
        }
        if (!(select.getUpdatetime() == null)) {
            select.setChoose_updatetime(true);
        }
        if (!"".equals(select.getStartprovince())) {
            select.setChoose_start_province(true);
            select.setChoose_pro_city_select(true);
        }
        if (!"".equals(select.getEndprovince())) {
            select.setChoose_end_province(true);
            select.setChoose_pro_city_select(true);
        }
//            select.setChoose_start_city(true);
//            select.setChoose_pro_city_select(true);
//            select.setChoose_end_city(true);
//            select.setChoose_pro_city_select(true);

        resp = Response.createBySuccess("select Bills", billdao.selectBill(select));
        
        return resp;
    }

    @Override
    public Response getAllProvince() {
        Response resp = null;
        List<MapProvince> list = new ArrayList<MapProvince>();
        list = billdao.getAllProvince();
        if (list.size() <= 0) {
            return Response.createByErrorMessage("Don't have any province");
        }
        resp = Response.createBySuccess("Province Infomation", list);
        return resp;
    }

    @Override
    public Response getAllCityByProvinceID(String proid) {
        Response resp = null;
        List<MapCity> list = new ArrayList<>();
        list = billdao.getAllCityByProID(proid);
        if (list.size() <= 0) {
            return Response.createByErrorMessage("Don't have any cities");
        }
        resp = Response.createBySuccess("City Infomation", list);
        return resp;
    }

    @Override
    public Response getAllDistrictCityID(String cityId) {
        Response resp = null;
        List<MapDistrict> list = new ArrayList<>();
        list = billdao.getAllDistrictByCityId(cityId);

        if (list.size() <= 0) {
            return Response.createByErrorMessage("Don't have any District");
        }

        resp = Response.createBySuccess("City Infomation", list);
        return resp;
    }

    @Override
    public Response getAllTownByDistrictID(String disid) {
        Response resp = null;
        List<MapTown> list = new ArrayList<>();
        list = billdao.getAllTownByDistrictID(disid);
        System.out.println("billdao" + list.get(0));
        if (list.size() <= 0) {
            return Response.createByErrorMessage("Don't have any towns");
        }
        resp = Response.createBySuccess("City Infomation", list);
        return resp;
    }

    @Override
    public MapProvince getProvinceByID(String id) {
        return billdao.getProvinceByID(id);
    }

    @Override
    public MapCity getCityByID(String id) {
        return billdao.getCityByID(id);
    }

    @Override
    public MapDistrict getDistrictByID(String id) {
        return billdao.getDistrictByID(id);
    }

    @Override
    public MapTown getTownByID(String id) {
        return billdao.getTownByID(id);
    }

    @Override
    public void saveCustomerInfo(CustomerInfo consignorinfo) {
        billdao.saveCustomerInfo(consignorinfo);
    }

    @Override
    public Response billForPage(int currentPage, int pageSize, int status) {
        Response resp = null;
        Page page = new Page();
        //总记录数
        int allRow = billdao.getAllRowCount();
        //当前页开始记录
        int offset = page.countOffset(currentPage, pageSize);
        //分页查询结果集
        List<Bill> list = billdao.listBillForPage(offset, pageSize, status);
//        for(Bill bill : list){
//            bill.setPassword(null);
//        }
        page.setPageNo(currentPage);
        page.setPageSize(pageSize);
        page.setTotalRecords(allRow);
        page.setList(list);
        resp = Response.createBySuccess(page);
        return resp;
    }

    @Override
    public Response passStatus0Bill(String BillId) {
        Response resp = null;
        Bill bill = billdao.getBillByID(BillId);
        if (bill == null) {
            return Response.createByErrorMessage("No Such Bill");
        }
        bill.setStatus(1);
        billdao.updateBill(bill);
        resp = Response.createBySuccessMessage("Pass Successful!");
        return resp;
    }

    @Override
    public Response passStatus1Bill(String BillId) {
        Response resp = null;
        Bill bill = billdao.getBillByID(BillId);
        if (bill == null) {
            return Response.createByErrorMessage("No Such Bill");
        }
        bill.setStatus(2);
        billdao.updateBill(bill);
        resp = Response.createBySuccessMessage("Pass Successful!");
        return resp;
    }

    //根据相应的区域查询相应的订单
    @Override
    public Response billForPageForNormal(int currentPage, int pageSize, int status, Employee emp) {
        Response resp = null;
        Page page = new Page();
        //总记录数
        int allRow = billdao.getAllRowCount();
        //当前页开始记录
        int offset = page.countOffset(currentPage, pageSize);
        //分页查询结果集
        List<Bill> list = null;
//        if (Authentication.isExpressAreaManager(emp)) {
//            MapTown town = billdao.getEmployeeMapTown(emp);
//            if (town == null) {
//                return Response.createByErrorMessage("Don't have any info");
//            }
//            List<CustomerInfo> areaCustom = billdao.getCustomerListInfoByMapTown(town);
//            if (areaCustom == null) {
//                return Response.createByErrorMessage("Don't have any CustomerInfo");
//            }
//            list = billdao.listNormalBillForPage(offset, offset, status, areaCustom);
//        } else if (Authentication.isExpressLogisticsManager(emp) || Authentication.isExpressAdmin(emp)) {
            list = billdao.listBillForPage(offset, pageSize, status);
//        }

        page.setPageNo(currentPage);
        page.setPageSize(pageSize);
        page.setTotalRecords(allRow);
        page.setList(list);
        resp = Response.createBySuccess(page);
        return resp;
    }

    @Override
    public Bill getBillById(String billID) {
        return billdao.getBillByID(billID);
    }

    @Override
    public Response billForPageForNormal(int currentPage, int pageSize, int status, Employee emp, Date createtimestart, Date createtimeend, Date updatetimestart, Date updatetimeend) {
        Response resp = null;
        Page page = new Page();
        //总记录数
        int allRow = billdao.getAllRowCount();
        //当前页开始记录
        int offset = page.countOffset(currentPage, pageSize);
        //分页查询结果集
        List<Bill> list = null;
        if (Authentication.isExpressAreaManagerOnly(emp)) {
            MapTown town = billdao.getEmployeeMapTown(emp);
            if (town == null) {
                return Response.createByErrorMessage("Don't have any info");
            }
            List<CustomerInfo> areaCustom = billdao.getCustomerListInfoByMapTown(town);
            if (areaCustom == null) {
                return Response.createByErrorMessage("Don't have any CustomerInfo");
            }
            list = billdao.listNormalBillForPage(offset, pageSize, status, areaCustom, createtimestart, createtimeend, updatetimestart, updatetimeend);
        } else if (Authentication.isExpressLogisticsManager(emp) || Authentication.isExpressAdmin(emp)) {
            list = billdao.listBillForPage(offset, pageSize, status);
        }
        page.setPageNo(currentPage);
        page.setPageSize(pageSize);
        page.setTotalRecords(allRow);
        page.setList(list);
        resp = Response.createBySuccess(page);
        return resp;
    }

    @Override
    public Response finishBill(String BillId) {//把完成的订单转成销单
        Response resp = null;
        Bill bill = billdao.getBillByID(BillId);
        if (bill == null) {
            return Response.createByErrorMessage("No Such Bill");
        }
        bill.setStatus(4);
        billdao.updateBill(bill);
        resp = Response.createBySuccessMessage("Finished Successful!");
        return resp;
    }

    @Override
    public Response getfinishedBillNum() {
        Response resp = null;
        int numofbills = billdao.getNumOfFinishedBill();
        resp = Response.createBySuccess(numofbills);
        return resp;

    }

    @Override
    public Response getNotfinishedBillNum() {
        Response resp = null;
        int numofbills = billdao.getNumOfNotFinishedBill();
        resp = Response.createBySuccess(numofbills);
        return resp;
    }

}
